package com.example.yuekaoceshi.moudle;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.example.yuekaoceshi.http.MyServer;
import com.example.yuekaoceshi.http.TypeApi;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by 段傅华 on 2018/12/11.
 */

public class Moudle implements Main_Moudle{

    @Override
    public void setdata(String url, Map<String, Object> map, Context context, final TypeApi typeApi, final Finish finish) {
        switch (typeApi){
            case LOGIN:
                UMShareAPI.get(context).getPlatformInfo((Activity) context, SHARE_MEDIA.QQ, new UMAuthListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {

                    }

                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                        finish.setData(map,typeApi);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        finish.setError(throwable.getMessage());
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {

                    }
                });
                break;
            case RLV:
                Retrofit build = new Retrofit.Builder()
                        .addConverterFactory(ScalarsConverterFactory.create())
                        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                        .baseUrl(MyServer.mUrl)
                        .build();
                MyServer myServer = build.create(MyServer.class);
                Observable<String> stringObservable = myServer.get(url, map);
                stringObservable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<String>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(String value) {
                                finish.setData(value,typeApi);
                            }

                            @Override
                            public void onError(Throwable e) {
                                finish.setError(e.getMessage());
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
                break;
        }
    }
}
